import { Controller } from 'egg';

export default class ProjectController extends Controller {

  /**
   * 创建数据
   */
  public async create() {
    const { w_validate_otherName, w_validate_description } = this.ctx
    this.ctx.validate({
      name: w_validate_otherName,
      description: w_validate_description,
    });
    await this.ctx.service.project.create(this.ctx.w_body('name description'));
    this.ctx.sendSuccess('OK');
  }

  /**
   * 获取列表数据
   */
  async index() {
    const { ctx } = this
    ctx.w_validate_page();
    const data = await Promise.all([ ctx.service.project.list(), ctx.service.project.count() ]);
    ctx.sendList(data);
  }

  /**
   * 获取所有数据
   */
  async all() {
    const { ctx } = this
    const data = await ctx.service.project.all()
    ctx.sendSuccess(data);
  }

  /**
   * 删除
   */
  async remove() {
    this.ctx.w_validate_param_id()
    const { id } = this.ctx.params
    await this.ctx.service.project.remove(id)
    this.ctx.sendSuccess('OK');
  }

  /**
   * 更新项目
   */
  async update() {
    this.ctx.w_validate_param_id()
    const { w_validate_otherName, w_validate_description } = this.ctx
    this.ctx.validate({
      name: w_validate_otherName,
      description: w_validate_description,
    });
    const { id } = this.ctx.params
    await this.ctx.service.project.update(id, this.ctx.w_body('name description'))
    this.ctx.sendSuccess('OK');
  }

  /**
   * 获取项目详情
   */
  async show() {
    this.ctx.w_validate_param_id()
    const { id } = this.ctx.params
    const model = await this.ctx.service.project.show(id)
    this.ctx.sendSuccess(model);
  }
  
}

